package mn.idax.exchange.otc.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mn.idax.exchange.auth.bean.AuthInfo;
import mn.idax.exchange.auth.util.AuthInfoUtils;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.annotation.LoginAuth;
import mn.idax.exchange.otc.constant.RequestUrlConstant;
import mn.idax.exchange.otc.domain.DictRequestDTO;
import mn.idax.exchange.otc.domain.ValidateQuota;
import mn.idax.exchange.otc.entity.OTCSysDictionaryItem;
import mn.idax.exchange.otc.enums.DictEnum;
import mn.idax.exchange.otc.enums.RequestSourceEnum;
import mn.idax.exchange.otc.service.OTCSysDictionaryItemService;
import mn.idax.exchange.otc.util.LogUtils;
import mn.idax.exchange.otc.util.ParamUtils;
import mn.idax.exchange.otc.vo.ADDefaultTipsDTO;
import mn.idax.exchange.otc.vo.PayTypeDTO;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Auther: shihaiming
 * @Date: 2018/08/10
 * @Description:
 */

@Api(value = "系统字典",tags={"系统字典"})
@RequestMapping(RequestUrlConstant.MODULE_SYS)
@RestController
public class OTCSysDictionaryController {

    @Autowired
    private OTCSysDictionaryItemService otcSysDictionaryItemService;
    
    /**

    /**
     * 获取支持的币种信息
     * @param request
     * @throws Exception
     */
    @ApiOperation(value = "获取支持的币种信息", notes = "获取支持的币种信息，(shihaiming，自测通过)")
    @RequestMapping(value = RequestUrlConstant.SYS_GET_COIN_CODE,method = RequestMethod.POST)
    @LoginAuth(disable = true)
    public ResponseData getCoinCode(HttpServletRequest request) throws Exception{
    	List<OTCSysDictionaryItem> otcSysDictionaryItem = otcSysDictionaryItemService.listItem(DictEnum.COIN_CODE.getCode() ,null);
    	//为app 端加的 2019-10-25
//    	if (!ParamUtils.getRequestSourceCode(request).equals(RequestSourceEnum.PC.getCode())){
//            otcSysDictionaryItem = otcSysDictionaryItem.subList(0,3);
//        }
        return ResponseData.success(otcSysDictionaryItem);
    }
    
    /**
     * 获取广告有效期天数
     * @param request
     * @throws Exception
     */
    @ApiOperation(value = "获取广告有效期天数", notes = "获取广告有效期天数，(shihaiming，自测通过)")
    @RequestMapping(value = RequestUrlConstant.SYS_GET_EXPIRE_DAY_NUM,method = RequestMethod.POST)
    @LoginAuth(disable = true)
    public ResponseData getExpireDayNum(HttpServletRequest request) throws Exception{
    	List<OTCSysDictionaryItem> otcSysDictionaryItem = otcSysDictionaryItemService.listItem(DictEnum.AD_EXPIRE_TIME.getCode(),null);
        return ResponseData.success(otcSysDictionaryItem);
    }
    
    /**
     * 获取广告默认提示语列表
     * @param request
     * @throws Exception
     */
    @ApiOperation(value = "获取广告默认提示语列表", notes = "获取广告默认提示语列表，(shihaiming，自测通过)")
    @RequestMapping(value = RequestUrlConstant.SYS_GET_DEFAULT_TIPS,method = RequestMethod.POST)
    public ResponseData getDefaultTips(HttpServletRequest request) throws Exception{
    	List<OTCSysDictionaryItem> defaultDescription = otcSysDictionaryItemService.listItem(DictEnum.AD_DEFAULT_DESCRIPTION.getCode(), ParamUtils.getLanguageByRequest(request));
    	List<OTCSysDictionaryItem> defaultRemark = otcSysDictionaryItemService.listItem(DictEnum.AD_DEFAULT_REMARK.getCode(),ParamUtils.getLanguageByRequest(request));
    	
    	ADDefaultTipsDTO adDefaultTipsDTO = new ADDefaultTipsDTO();
    	adDefaultTipsDTO.setDefaultDescription(defaultDescription);
    	adDefaultTipsDTO.setDefaultRemark(defaultRemark);
        return ResponseData.success(adDefaultTipsDTO);
    }
    
    /**
     * 获取申诉类型
     * @param request
     * @throws Exception
     */
    @ApiOperation(value = "获取申诉类型", notes = "获取申诉类型，(shihaiming，自测通过)")
    @RequestMapping(value = RequestUrlConstant.SYS_GET_COMPLAINT_TYPE,method = RequestMethod.POST)
    @LoginAuth(disable = true)
    public ResponseData getComplaintType(HttpServletRequest request) throws Exception{
    	List<OTCSysDictionaryItem> otcSysDictionaryItem = otcSysDictionaryItemService.listItem(DictEnum.COMPLAINT_TYPE.getCode() , ParamUtils.getLanguageByRequest(request));
        return ResponseData.success(otcSysDictionaryItem);
    }

    /**
     * 通过字典编码获取字典详情列表
     * @param dict
     * @throws Exception
     */
    @ApiOperation(value = "获取字典详情列表（通过字典编码）", notes = "获取字典详情列表（通过字典编码），(dison，自测通过)")
    @RequestMapping(value = RequestUrlConstant.SYS_LIST_ITEM,method = RequestMethod.POST)
    public ResponseData listItem(@RequestBody DictRequestDTO dict , HttpServletRequest request) throws Exception{
        List<OTCSysDictionaryItem> otcSysDictionaryItem = otcSysDictionaryItemService.listItem(dict.getDictCode() , ParamUtils.getLanguageByRequest(request));
        return ResponseData.success(otcSysDictionaryItem);
    }


//    @ApiOperation(value = "获取法币支持的支付方式", notes = "v 2.01 弃用(jack_huang，自测通过)")
//    @RequestMapping(value = RequestUrlConstant.AD_PAY_METHODS,method = RequestMethod.POST)
//    public ResponseData getPayMethod(@RequestBody Unit unit){
//
//        String payMethodUnit = unit.getUnit();
////        if (StringUtils.isEmpty(payMethodUnit)){
////            throw new IdaxException(OTCAdResponseEnum.PAYMETHOD_UNIT_EMPTY.getCode());
////        }
//        Integer payMethod = OTCBeneficiarysEnum.PayUnitEnum.getPayUnit(payMethodUnit);
//        if (payMethod == null){
//            throw new IdaxException(OTCAdResponseEnum.PAYMETHOD_UNIT_ERROR.getCode());
//        }
//        List<OTCSysDictionaryItem> listItem = otcSysDictionaryItemService.listItem(DictEnum.AD_PAY_METHOD.getCode());
//        OTCSysDictionaryItem buyAdMaxLimit = otcSysDictionaryItemService.getItemByDictIdAndItemValue(listItem.get(0).getDictid(), payMethod);
//
//        return ResponseData.success(buyAdMaxLimit);
//    }

    @ApiOperation(value = "获取支持的法币", notes = "获取支持的法币，(jack_huang，自测通过)")
    @RequestMapping(value = RequestUrlConstant.AD_PAY_UNIT,method = RequestMethod.POST)
    public ResponseData getPayUnit(){
        List<OTCSysDictionaryItem> otcSysDictionaryItem = otcSysDictionaryItemService.listItem(DictEnum.AD_PAY_UNIT.getCode() ,null);
        return ResponseData.success(otcSysDictionaryItem);
    }

    @ApiOperation(value = "出售广告时获取最小限额和最小限额", notes = "出售广告时获取最小限额和最小限额，(jack_huang，自测通过)")
    @RequestMapping(value = RequestUrlConstant.QUOTA_AMOUNT,method = RequestMethod.POST)
    public ResponseData getQuotaAmount(@RequestBody ValidateQuota validateQuota){

        Map<String,String> map = null;
        try {
            LogUtils.info(this.getClass(),"getQuotaAmount method start ******");
            AuthInfo authInfo = AuthInfoUtils.getCurrent();
            Integer userId = Optional.ofNullable(authInfo).map(a -> a.getId()).orElse(-1);
            map = otcSysDictionaryItemService.getMinamountAndMaxAmount(validateQuota,userId);
        } catch (Exception e) {
            LogUtils.info(this.getClass(),"quotaAmount exception:"+ ExceptionUtils.getStackTrace(e));
        }
        return ResponseData.success(map);
    }

    @ApiOperation(value = "场外支持的支付方式", notes = "场外支持的支付方式，(jack_huang，自测通过)")
    @RequestMapping(value = RequestUrlConstant.PAY_TYPE,method = RequestMethod.POST)
    public ResponseData getPayType(){
        List<PayTypeDTO> payTypes = otcSysDictionaryItemService.getPayType();
        return ResponseData.success(payTypes);
    }
    
}
